package com.littleken.util;


import com.littleken.config.DBConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 数据库连接工具类
 * littleken
 */
public class DBUtil {

    public enum MyEnumSingleton {

        connectionFactory;
        private Connection conn;

        private MyEnumSingleton() {
            try {
                String url = DBConfig.MYSQL_URL;
                String name = DBConfig.MYSQL_USERNAME;
                String password = DBConfig.MYSQL_USERPASSWORD;
                String Driver = DBConfig.MYSQL_DRIVER;

                Class.forName(Driver);

                conn = DriverManager.getConnection(url, name, password);
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        public Connection getConn() {
            return conn;
        }

    }

    /**
     * 打开数据库连接
     *littleken
     * @return Connection
     */
    public static Connection getConn() {
        return MyEnumSingleton.connectionFactory.getConn();
    }

    /**
     * 关闭数据库连接
     * littleken
     */
    public static void close() {
        if (getConn() != null) {
            try {
                getConn().close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
